//
//  NoteBookCell.m
//  Grammar
//
//  Created by Rick on 6/30/13.
//
//

#import "NoteBookCell.h"
#import "Note.h"
#import "NoteBookManager.h"
#import "ITTImageCacheManager.h"

@implementation NoteBookCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc {
    [_currentNote release];
    [_noteImageView release];
    [_contentLabel release];
    [_updateTimeLabel release];
    [_moreButton release];
    [super dealloc];
}

- (void)awakeFromNib
{
    _noteImageView.delegate = self;
}

- (CGFloat)getCellHeight
{
    return _cellHeight;
}

- (void)setCellrow:(NSInteger)cellrow
{
    _cellRow = cellrow;
}

- (void)setCellWithNote:(Note*)note
{
    
    [_noteImageView setEnableTapEvent:YES];
    
    
    _currentNote = [note retain];
    
    if (note.image.length > 0 && note.content.length > 0) {
        [self layoutCellStyle:NoteBookCellTypeWithCotentWithImage];
        UIImage *image = [[ITTImageCacheManager sharedManager] getImageFromCacheWithUrl:note.image];
        if (image) {
            ITTDINFO(@"get image!");
        }
        _noteImageView.image = image;
        _contentLabel.text = note.content;
        
        CGSize labelSize = [note.content sizeWithFont:_contentLabel.font constrainedToSize:CGSizeMake(_contentLabel.width, 300) lineBreakMode:UILineBreakModeWordWrap];
        _contentLabel.height = labelSize.height;
        _updateTimeLabel.top = _contentLabel.bottom ;
        _cellHeight = 60 + labelSize.height + 25;
        
    }else if(note.image.length > 0 && !note.content.length > 0) {
        [self layoutCellStyle:NoteBookCellTypeWithImageOutContent];

        UIImage *image = [[ITTImageCacheManager sharedManager] getImageFromCacheWithUrl:note.image];
        if (image) {
            ITTDINFO(@"get image!");
        }
        if (image) {
            _noteImageView.image = image;
        }else {
            [_noteImageView loadImage:note.image];
        }
        _updateTimeLabel.top = _noteImageView.bottom + 1;
        _cellHeight = 84;
    }else if(!note.image.length > 0 && note.content.length > 0) {
        [self layoutCellStyle:NoteBookCellTypeWithCotentOutImage];
        
        _contentLabel.text = note.content;
        CGSize labelSize = [note.content sizeWithFont:_contentLabel.font constrainedToSize:CGSizeMake(_contentLabel.width, 300) lineBreakMode:UILineBreakModeWordWrap];
        _contentLabel.top = 10;
        
        _contentLabel.height = labelSize.height;
        _cellHeight = 10 + labelSize.height + 25;
        _updateTimeLabel.top = _contentLabel.bottom + 1;
    }
    
    _updateTimeLabel.text = [NSString stringWithFormat:@"%@-%@-%@  %@:%@",[note.noteCreateTimeStamp substringToIndex:4],[note.noteCreateTimeStamp substringWithRange:NSMakeRange(4,2)],[note.noteCreateTimeStamp substringWithRange:NSMakeRange(6,2)],[note.noteCreateTimeStamp substringWithRange:NSMakeRange(8,2)],[note.noteCreateTimeStamp substringWithRange:NSMakeRange(10,2)]] ;
    self.height = _cellHeight;
    _moreButton.centerY = _cellHeight/2;
}

- (void)layoutCellStyle:(NoteBookCellType)type
{
    switch (type) {
        case NoteBookCellTypeWithCotentOutImage:
            _noteImageView.hidden = YES;
            _contentLabel.hidden = NO;
            break;
        case NoteBookCellTypeWithCotentWithImage:
            _noteImageView.hidden = NO;
            _contentLabel.hidden = NO;
            _contentLabel.top = _noteImageView.bottom +10;
            break;
        case NoteBookCellTypeWithImageOutContent:
            _noteImageView.hidden = NO;
            _contentLabel.hidden = YES;
            _updateTimeLabel.top = _noteImageView.bottom +5;
            break;
        default:
            break;
    }
}

#pragma mark - ITTImageViewDelegate
- (void)imageClicked:(ITTImageView *)imageView
{
    if (_delegate && [_delegate respondsToSelector:@selector(selectNoteBook:)]) {
        [_delegate selectNoteBook:_currentNote];
    }
}

- (void)imageLoaded:(ITTImageView *)imageView
{
    _noteImageView.image = imageView.image;
}
- (IBAction)tapMoreOptionButton:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(tapMoreOption:withIndex:)]) {
        [_delegate tapMoreOption:_currentNote withIndex:_cellRow];
    }
}
@end
